package com.diandiji.demo.service.impl;

import com.diandiji.demo.dao.NoteDao;
import com.diandiji.demo.entity.Note;
import com.diandiji.demo.service.NoteService;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;


@Service // 标识为Spring的Service组件
public class NoteServicelmpl implements NoteService {
    @Resource // 按名称自动注入NoteDao的bean对象
    private NoteDao noteDao;


    @Override
    public List<Note> queryAllNote(int userID) {
        return noteDao.queryAllNote();
    }

    @Override
    public List<Note> queryNoteByTagOrTitle(String tag,String title,int userID) {
        System.out.println("[Service日志] 搜索参数：tag=" + tag + ", title=" + title + ", userID=" + userID);
        return noteDao.queryNoteByTagOrTitle(tag, title, userID);
    }

    @Override
    public List<Note> queryNoteByAreaID(int areaID, int userID) {
        return noteDao.queryNoteByAreaID(areaID, userID);
    }

    @Override
    public void addNote(Note note) {
        // 设置创建日期为当前时间
        note.setNotetime(new Date());
        noteDao.insert(note);
    }

    @Override
    public void updateNote(Note note) {
        // 保留原有的创建日期
        Note existingNote = noteDao.queryByID(note);
        if (existingNote != null) {
            note.setNotetime(existingNote.getNotetime());
        }
        noteDao.update(note);
    }

    @Override
    public Note getNoteById(int noteId) {
        Note note = new Note();
        note.setNotenum(noteId);
        return noteDao.queryByID(note);
    }

    @Override
    public void deleteNote(int noteId) {
        noteDao.deleteById(noteId);
    }

}

